<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<style>
    .layui-table-cell {
        height: inherit;
    }
</style>
<body>
<div class="layui-fluid">
    <fieldset class="layui-elem-field">
        <legend>商品管理</legend>
        <div class="layui-field-box">
            <div class="layui-card">
                <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
                    <ul class="layui-tab-title">
                        <li class="layui-this" lay-id="1">正常产品({$count[0]})</li>
                        <li lay-id="2">回收站产品({$count[1]})</li>
                    </ul>
                    <div class="layui-tab-content">
                        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">商品名称</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="name" placeholder="可模糊搜索" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">所属分类</label>
                                    <div class="layui-input-inline">
                                        <select name="cid">
                                            <option value="">不限</option>
                                            {volist name='cate' id='vo'}
                                            <option value="{$vo.id}">{$vo.name}</option>
                                            {volist name='vo.lower' id='voo'}
                                            <option value="{$voo.id}">&nbsp;&nbsp;&nbsp;{$voo.name}</option>
                                            {/volist}
                                            {/volist}
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">状态</label>
                                    <div class="layui-input-inline">
                                        <select name="zhuang">
                                            <option value="">全部</option>
                                            <option value="1">销售中</option>
                                            <option value="2">已售完</option>
                                            <option value="3">已下架</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn layuiadmin-btn-list" id="btn" lay-submit lay-filter="LAY-app-contlist-search1">
                                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                                    </button>
                                </div>
                            </div>
                        </div>
                        <div class="layui-tab-item layui-show">
                            <div class="layui-card-body higo-card">
                                <table id="demo" lay-filter="demo" lay-skin="nob"></table>
                            </div>
                        </div>
                        <div class="layui-tab-item">
                            <div class="layui-card-body">
                                <table id="demo2" lay-filter="demo2" lay-skin="nob"></table>
                            </div>
                        </div>
                    </div>
                </div>
                <script type="text/html" id="name">
                    <div style="display: flex">
                        <img class="imgs" src="/uploads/{{d.img}}" title="{{d.name}}" style="width: 60px;height: 60px;cursor: pointer;">
                        <div style="margin-left: 15px;">
                            <div style="flex-wrap: wrap;">{{d.name}}</div>
                            <div style="color: #f60;">¥ {{d.price}}</div>
                        </div>
                    </div>
                </script>
                <script type="text/html" id="time">
                    {{ formatDateTime(d.time) }}
                </script>
                <script type="text/html" id="status">
                    {{# if (d.status == 1){ }}
                    <button class="layui-btn layui-btn-danger layui-btn-xs">已下架</button>
                    {{# }else{ }}
                    {{# if (d.stock > 0){ }}
                    <button class="layui-btn layui-btn-normal layui-btn-xs">销售中</button>
                    {{# }else{ }}
                    <button class="layui-btn layui-btn-warm layui-btn-xs">已售完</button>
                    {{# } }}
                    {{# } }}
                </script>
                <script type="text/html" id="table-content-list0">
                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-href="/admin/product/product_operate/id/{{d.id}}"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                    <a class="layui-btn layui-btn-normal layui-btn-xs operation">操作<i class="layui-icon layui-icon-triangle-d"></i></a>
                    <ul class="layui-nav-child layui-anim layui-anim-upbit" style="padding: 10px;top: 22px;min-width: inherit;position: absolute;display: none;">
                        <li>
                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="bargain"><i class="iconfont">&#xe665;</i>开启砍价</a>
                        </li>
                        <li>
                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="spell"><i class="iconfont">&#xe7d9;</i>开启拼团</a>
                        </li>
                        <li>
                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="seckill"><i class="iconfont">&#xe725;</i>开启秒杀</a>
                        </li>
                        <li>
                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="back"><i class="iconfont">&#xe613;</i>开启全返</a>
                        </li>
                        <li>
                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="shopping"><i class="iconfont">&#xe61c;</i>开启N元购</a>
                        </li>
                        <li>
                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="likes"><i class="iconfont">&#xe600;</i>开启点赞</a>
                        </li>
                        <li>
                            {{# if(d.status == 0){ }}
                            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="down"><i class="layui-icon layui-icon-tread"></i>下架</a>
                            {{# }else{ }}
                            <a class="layui-btn layui-btn-xs" lay-event="up"><i class="layui-icon layui-icon-praise"></i>上架</a>
                            {{# } }}
                        </li>
                        <li>
                            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="soft_del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                        </li>
                    </ul>
                </script>
                <script type="text/html" id="table-content-list1">
                    <a class="layui-btn layui-btn-xs" lay-event="recover">恢复产品</a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>彻底删除</a>
                </script>
            </div>
        </div>
    </fieldset>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table', 'element'], function(){
        var $ = layui.$,
                form = layui.form,
                table = layui.table,
                element = layui.element;

        var layid = location.hash.replace(/^#Tab=/, '');
        element.tabChange('docDemoTabBrief', layid);
        $("#btn").attr('lay-filter', 'LAY-app-contlist-search' + layid);

        element.on('tab(docDemoTabBrief)', function(data){
            var index = data.index;
            $("#btn").attr('lay-filter', 'LAY-app-contlist-search' + $(this).attr('lay-id'));
            location.hash = 'Tab='+ $(this).attr('lay-id');
        });

        //监听正常产品搜索
        form.on('submit(LAY-app-contlist-search1)', function(data){
            var field = data.field;
            //执行重载
            table.reload('demo', {
                url: "{:url('Product/product')}",
                where: field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        //监听回收站产品搜索
        form.on('submit(LAY-app-contlist-search2)', function(data){
            var field = data.field;
            //执行重载
            table.reload('demo2', {
                url: "{:url('Product/product', ['is_del' => 1])}",
                where: field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        //点击放大图片
        $(document).on('click', '.imgs', function (){
            var img = $(this).attr('src');
            layer.open({
                type: 1,
                title: false,
                closeBtn: 0,
                area: ['auto', 'auto'],
                offset: ['20%', '30%'],
                skin: 'layui-layer-nobg', //没有背景色
                shadeClose: true,
                content: '<img src="' + img + '" style="width: 400px;height: 400px;">'
            });
        });

        //正常产品执行渲染
        table.render({
            elem: '#demo',
            url: "{:url('Product/product')}",
            method: 'post',
            cols: [[
                {field: 'name', title: '商品名称', templet: "#name"},
                {field: 'stock', title: '库存', width: 120},
                {field: 'volume', title: '销量', width: 120},
                {field: 'time', title: '添加时间', width: 250, templet: "#time"},
                {field: 'status', title: '状态 ', width: 120, templet: "#status"},
                {fixed: 'right', title: '操作', width: 150, align:'center', toolbar: '#table-content-list0'}
            ]],
            page: {
                limits: [10, 20, 30, 40, 50]
            }
        });

        //回收站产品执行渲染
        table.render({
            elem: '#demo2',
            url: "{:url('Product/product', ['is_del' => 1])}",
            method: 'post',
            cols: [[
                {field: 'name', title: '商品名称', templet: "#name"},
                {field: 'stock', title: '库存', width: 120},
                {field: 'volume', title: '销量', width: 120},
                {field: 'time', title: '添加时间', width: 250, templet: "#time"},
                {field: 'status', title: '状态 ', width: 120, templet: '<div><button class="layui-btn layui-btn-danger layui-btn-xs">已删除</button></div>'},
                {fixed: 'right', title: '操作', width: 250, align:'center', toolbar: '#table-content-list1'}
            ]],
            page: {
                limits: [10, 20, 30, 40, 50]
            }
        });

        //监听事件
        table.on('tool(demo)', function(obj){
            var id = obj.data.id;
            var layEvent = obj.event;

            if (layEvent === 'bargain'){ //开启砍价
                parent.layer.open({
                    type: 2,
                    title: '开启砍价',
                    content: '/admin/Market/bargain_operate/pid/' + id,
                    maxmin: true,
                    area: ['700px', '700px'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    success: function (layero, index){
                        // 获取子页面的iframe
                        var iframe = parent['layui-layer-iframe' + index];
                        // 向子页面的全局函数child传参
                        iframe.child(window);
                    },
                    yes: function (index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            } else if (layEvent === 'spell'){ //拼团
                parent.layer.open({
                    type: 2,
                    title: '开启拼团',
                    content: '/admin/Market/spell_operate/pid/' + id,
                    maxmin: true,
                    area: ['700px', '600px'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    success: function (layero, index){
                        // 获取子页面的iframe
                        var iframe = parent['layui-layer-iframe' + index];
                        // 向子页面的全局函数child传参
                        iframe.child(window);
                    },
                    yes: function (index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            } else if (layEvent === 'back'){ //全返
                parent.layer.open({
                    type: 2,
                    title: '开启全返',
                    content: '/admin/Market/back_operate/pid/' + id,
                    maxmin: true,
                    area: ['600px', '300px'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    success: function (layero, index){
                        // 获取子页面的iframe
                        var iframe = parent['layui-layer-iframe' + index];
                        // 向子页面的全局函数child传参
                        iframe.child(window);
                    },
                    yes: function (index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            } else if (layEvent === 'seckill'){ //秒杀
                parent.layer.open({
                    type: 2,
                    title: '开启秒杀',
                    content: '/admin/Market/seckill_operate/pid/' + id,
                    maxmin: true,
                    area: ['700px', '600px'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    success: function (layero, index){
                        // 获取子页面的iframe
                        var iframe = parent['layui-layer-iframe' + index];
                        // 向子页面的全局函数child传参
                        iframe.child(window);
                    },
                    yes: function (index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            } else if (layEvent === 'shopping'){ //N元购
                parent.layer.open({
                    type: 2,
                    title: '开启N元购',
                    content: '/admin/Market/purchase_operate/pid/' + id,
                    maxmin: true,
                    area: ['600px', '400px'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    success: function (layero, index){
                        // 获取子页面的iframe
                        var iframe = parent['layui-layer-iframe' + index];
                        // 向子页面的全局函数child传参
                        iframe.child(window);
                    },
                    yes: function (index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            } else if (layEvent === 'likes'){ //点赞
                parent.layer.open({
                    type: 2,
                    title: '开启点赞活动',
                    content: '/admin/Market/likes_operate/pid/' + id,
                    maxmin: true,
                    area: ['700px', '600px'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    success: function (layero, index){
                        // 获取子页面的iframe
                        var iframe = parent['layui-layer-iframe' + index];
                        // 向子页面的全局函数child传参
                        iframe.child(window);
                    },
                    yes: function (index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            } else if (layEvent === 'soft_del'){ //删除
                layer.confirm('确定删除到回收站吗?', {
                    icon: 3,
                    title:'提示',
                    offset: ['25%', '35%']
                }, function(){
                    $.post("{:url('Product/product_soft_del')}", {id:id,del:1}, function(e){
                        if (e.code == 200){
                            layer.msg(e.data, {offset: ['30%', '40%']});
                            setTimeout(function (){
//                                table.reload('demo');
                                location.reload();
                            }, 500);
                        }else{
                            layer.msg(e.msg, {offset: ['30%', '40%']});
                        }
                    }, 'json');
                });
            } else if (layEvent === 'up'){ //上架
                var index = layer.load(1, {offset: ['30%', '42%']});
                $.post("{:url('Product/up_and_down')}", {id:id,status:'0'}, function (e){
                    layer.close(index);
                    if (e == 1){
                        layer.msg('上架成功', {offset: ['30%', '40%']});
                        table.reload('demo');
                    }else{
                        layer.msg('上架失败', {offset: ['30%', '40%']});
                    }
                });
            } else if (layEvent === 'down'){ //下架
                var index2 = layer.load(1, {offset: ['30%', '42%']});
                $.post("{:url('Product/up_and_down')}", {id:id,status:'1'}, function (e){
                    layer.close(index2);
                    if (e == 1){
                        layer.msg('下架成功', {offset: ['30%', '40%']});
                        table.reload('demo');
                    }else{
                        layer.msg('下架失败', {offset: ['30%', '40%']});
                    }
                });
            } else if (layEvent === 'download'){ //下载文件
                var code = obj.data.sp_code;
                var name = obj.data.name;
                window.open('/common/Common/download.html?code=' + code + '&name=' + name);
            }
        });

        //监听事件
        table.on('tool(demo2)', function(obj){
            var id = obj.data.id;
            var layEvent = obj.event;

            if (layEvent === 'recover'){ //删除
                layer.confirm('确定恢复产品吗?', {
                    icon: 3,
                    title:'提示',
                    offset: ['25%', '35%']
                }, function(){
                    $.post("{:url('Product/product_soft_del')}", {id:id,del:0}, function(e){
                        if (e.code == 200){
                            layer.msg(e.data, {offset: ['30%', '40%']});
                            setTimeout(function (){
//                                table.reload('demo2');
                                location.reload();
                            }, 500);
                        }else{
                            layer.msg(e.msg, {offset: ['30%', '40%']});
                        }
                    }, 'json');
                });
            } else if (layEvent === 'del'){ //彻底删除
                layer.confirm('确定彻底删除吗?', {
                    icon: 3,
                    title:'提示',
                    offset: ['25%', '35%']
                }, function(){
                    $.post("{:url('Product/product_del')}", {id:id}, function(e){
                        if (e.code == 200){
                            layer.msg(e.data, {offset: ['30%', '40%']});
                            setTimeout(function (){
//                                table.reload('demo2');
                                location.reload();
                            }, 500);
                        }else{
                            layer.msg(e.msg, {offset: ['30%', '40%']});
                        }
                    }, 'json');
                });
            }
        });

        //事件
        var active = {
            jiance: function(){
                var index = layer.load();
                $.post("{:url('Product/sp_code')}", function(e){
                    layer.close(index);
                    layer.msg(e.msg);
                }, 'json');
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });

    //php时间戳用js转换为yyyy-mm-dd
    function formatDateTime(timeStamp) {
        var date = new Date();
        date.setTime(timeStamp * 1000);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
    }
</script>
</body>
</html>
